package com.yanwq.sayhi.controller.club.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yanwq.pulltorefreshview.view.PullToLoadListener;
import com.yanwq.pulltorefreshview.view.PullToRefreshLayout;
import com.yanwq.pulltorefreshview.view.PullToRefreshListView;
import com.yanwq.pulltorefreshview.view.PullToRefreshListener;
import com.yanwq.pulltorefreshview.view.PullToRefreshRecyclerView;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseToolbarActivity;
import com.yanwq.sayhi.common.client.OkJsonArrayResponseCallback;
import com.yanwq.sayhi.common.client.OkRequestParams;
import com.yanwq.sayhi.common.client.OkRestClient;
import com.yanwq.sayhi.controller.club.adapter.ClubAdapter;
import com.yanwq.sayhi.controller.user.UserMgr;
import com.yanwq.sayhi.dao.bean.Club;

import java.util.List;

import butterknife.BindView;

public class ClubListActivity extends BaseToolbarActivity {
    @BindView(R.id.club_empty)
    View mVEmpty;
    @BindView(R.id.club_refresh)
    PullToRefreshLayout refreshLayout;
    @BindView(R.id.club_list)
    PullToRefreshRecyclerView listView;

    private ClubAdapter mAdapter;
    private int mPage;
    private int mSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_list);
    }

    private void autoRefresh() {
        post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.startRefresh();
            }
        });
    }

    @Override
    public void init() {
        mPage = 0;
        mSize = 20;
        mAdapter = new ClubAdapter();
    }

    @Override
    public void initView() {
        mToolbar.setTitle("所有俱乐部");

        refreshLayout.setPullToRefreshListener(new PullToRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        refreshLayout.setPullToLoadListener(new PullToLoadListener() {
            @Override
            public void onLoad() {
                load();
            }
        });

        listView.setLayoutManager(new LinearLayoutManager(this));
        listView.setAdapter(mAdapter);
        autoRefresh();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_club_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return true;
    }

    private void refresh() {
        mPage = 0;
        OkRequestParams params = new OkRequestParams();
        params.put("userId", UserMgr.get().getId());
        params.put("page", mPage);
        params.put("size", mSize);
        OkRestClient.getInstance().get(this, "/club/list", params, new OkJsonArrayResponseCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(JSONArray jsonArray) {
                List<Club> clubs = JSON.parseArray(jsonArray.toJSONString(), Club.class);
                mAdapter.refresh(clubs);

                if (clubs.size() > 0) {
                    mVEmpty.setVisibility(View.GONE);
                } else {
                    mVEmpty.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onStop() {
                refreshLayout.stopRefresh();
            }
        });
    }

    private void load() {
        mPage++;
        OkRequestParams params = new OkRequestParams();
        params.put("userId", UserMgr.get().getId());
        params.put("page", mPage);
        params.put("size", mSize);
        OkRestClient.getInstance().get(this, "/club/list", params, new OkJsonArrayResponseCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(JSONArray jsonArray) {
                List<Club> clubs = JSON.parseArray(jsonArray.toJSONString(), Club.class);
                mAdapter.load(clubs);
            }

            @Override
            public void onStop() {
                refreshLayout.stopLoad();
            }
        });
    }
}
